package com.pl.lister;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.pl.entity.WmStorageLocation;
import com.pl.service.WmStorageLocationService;

import java.util.ArrayList;
import java.util.List;

/**
 * @version 1.0
 * @Author 小胤
 * @Date 2024/4/28 9:37
 * @注释
 */
public class WmStorageLocationReadLocation extends AnalysisEventListener<WmStorageLocation> {
    //定义批量导入的时候 每次导入几条数据
    private static final int BATCH_COUNT=5;
    List<WmStorageLocation> list=new ArrayList<>();
    private WmStorageLocationService wmStorageLocationService;

    public WmStorageLocationReadLocation() {
    }

    public WmStorageLocationReadLocation(WmStorageLocationService wmStorageLocationService) {
        this.wmStorageLocationService = wmStorageLocationService;
    }

    @Override
    public void invoke(WmStorageLocation wmStorageLocation, AnalysisContext analysisContext) {
        list.add(wmStorageLocation);
        if (list.size()>=BATCH_COUNT){
            saveData();
            list.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        if (list.size()>0){
            saveData();
            list.clear();
        }
    }
    public void saveData(){
        System.out.println("开始批量导入");
        wmStorageLocationService.insertBatch(list);
        System.out.println("结束批量导入");
    }
}
